<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\KillDurgsModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;

class KillDrugsController extends BaseController
{
    /**
     * @var array
     *  展示秒杀药品列表
     *  ww
     */
    public function KilList()
    {
         try {
             $data=KillDurgsModel::all();
            return $this->witejson(0,$data,'ok');
            }catch (\Exception $e){
              return $this->witejson(1,'',$e->validator->errors()->first());
           }
    }

    /**
     * @param Request $request
     * @return mixed
     * 商品预热(添加)
     * ww
     */
    public function AddKill(Request $request){
        try {
            $data = $request->validate([
                'name' => 'required|unique:kill,name',
                'num' => 'required',
                'nownum' => 'required',
                'start_time' => 'required',
                'end_time' => 'required',
                'image' => 'required',
                'title' => 'required',
                'status' => 'required',
                'price' => 'required',
                'nowprice' => 'required',
            ]);

            $name = $data['name'];
            $res = Kill::KillCreat($data);
            if (!$res) throw new \Exception('数据添加失败');

            $data = Kill::KillSelect(['name'=>$name]);
            $data = json_decode(json_encode($data),true);

            $id = Redis::get($data['id']);
            Redis::select(3);
            $key = 'goods_'.$data['id'];
            if (empty($id)){
                for ($i=1;$i<=$data['num'];$i++){
                    Redis::lpush($key,$i);
                }
            }
            Redis::select(4);
            $datas = json_encode($data,true);
            Redis::set($key,$datas);
            return comment::success(20000,'成功',$data);
        }catch (\Exception $exception){
            return comment::error(2001,'',$exception->getMessage());
        }
    }





}
